package cn.jhc.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import uuntil.Constants;
import cn.jhc.bean.Song;
import cn.jhc.db.SongDao;

/**
 * Servlet implementation class AddSongServlet
 */
@WebServlet("/addsong.do")
public class AddSongServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public AddSongServlet() {
        super();
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DataSource dataSource = (DataSource)request.getServletContext().getAttribute(Constants.H2_DATASOURCE);
		request. setCharacterEncoding("UTF-8");
		String songn=request.getParameter("songname");
		String songsinger=request.getParameter("songsinger");
		String songimage=request.getParameter("songimage");
		String songURL=request.getParameter("songURL");
		String songwords=request.getParameter("songwords");
		Song song=new Song(songn,songsinger,songimage,songwords,songURL);
		SongDao.addSong(dataSource,song);
//		try {
//			SongDao.deleteAllSong(dataSource);
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		response.sendRedirect("./listsong.do");
		
	}

}
